﻿namespace GamePlus.OpenAuth.V20
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;

    internal static class Utility
    {
        public static byte[] BuildPostData(string boundary, params WeiboParameter[] parameters)
        {
            List<WeiboParameter> list = parameters.OrderBy<WeiboParameter, string>(delegate (WeiboParameter p) {
                return p.Name;
            }).ToList<WeiboParameter>();
            string str = GetBoundary();
            string str2 = string.Format("--{0}", boundary);
            string str3 = string.Format("--{0}--", boundary);
            string name = "iso-8859-1";
            StringBuilder builder = new StringBuilder();
            foreach (WeiboParameter parameter in list)
            {
                if (parameter is WeiboStringParameter)
                {
                    WeiboStringParameter parameter2 = parameter as WeiboStringParameter;
                    builder.AppendLine(str2);
                    builder.AppendLine(string.Format("content-disposition: form-data; name=\"{0}\"", parameter2.Name));
                    builder.AppendLine();
                    builder.AppendLine(HttpUtility.UrlEncode(parameter2.Value).Replace("+", "%20"));
                }
                else
                {
                    WeiboBinaryParameter parameter3 = parameter as WeiboBinaryParameter;
                    builder.AppendLine(str2);
                    builder.AppendLine(string.Format("content-disposition: form-data; name=\"{0}\"; filename=\"{1}\"", parameter3.Name, string.Format("upload{0}", BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0))));
                    builder.AppendLine("Content-Type: image/unknown");
                    builder.AppendLine("Content-Transfer-Encoding: binary");
                    builder.AppendLine();
                    builder.AppendLine(Encoding.GetEncoding(name).GetString(parameter3.Value));
                }
            }
            builder.Append(str3);
            return Encoding.GetEncoding(name).GetBytes(builder.ToString());
        }

        public static string BuildQueryString(params WeiboParameter[] parameters)
        {
            List<string> list = new List<string>();
            foreach (WeiboParameter parameter in parameters)
            {
                if (parameter is WeiboStringParameter)
                {
                    list.Add(string.Format("{0}={1}", HttpUtility.UrlEncode(parameter.Name), HttpUtility.UrlEncode(((WeiboStringParameter) parameter).Value)));
                }
            }
            return string.Join("&", list.ToArray());
        }

        public static string BuildQueryString(Dictionary<string, string> parameters)
        {
            List<string> list = new List<string>();
            foreach (KeyValuePair<string, string> pair in parameters)
            {
                list.Add(string.Format("{0}={1}", HttpUtility.UrlEncode(pair.Key), HttpUtility.UrlEncode(pair.Value)));
            }
            return string.Join("&", list.ToArray());
        }

        public static string GetBoundary()
        {
            return HttpUtility.UrlEncode(Convert.ToBase64String(Guid.NewGuid().ToByteArray()));
        }
    }
}

